package appapi.service.impl;

import appapi.Request.ModulelistReq;
import appapi.Request.RolePageReq;
import appapi.Request.RoleReq;
import appapi.dao.RoleMapper;
import appapi.dao.RolepermissionMapper;
import appapi.entity.auth.RoleEntity;
import appapi.service.RoleService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色信息
 *
 * @author lc
 * @version 1.0
 * @date 2023-05-17 10:08:27
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolepermissionMapper roleAuthMapper;

    @Override
    public PageInfo<RoleEntity> page(RolePageReq req) {
        if (ObjectUtils.allNotNull(req.getCurrPage(), req.getPageSize())) {
            PageHelper.startPage(req.getCurrPage(), req.getPageSize());
        }
        String json = JSON.toJSONString(req);
        RoleReq dataReq = JSON.parseObject(json, new TypeReference<RoleReq>() {
        });
        PageInfo<RoleEntity> pageInfo = PageHelper.startPage(req.getCurrPage(), req.getPageSize())
                .doSelectPageInfo(() -> this.list(dataReq));

        return pageInfo;
    }

    @Override
    public List<RoleEntity> list(RoleReq roleReq) {
        return roleMapper.list(roleReq);
    }

    @Override
    public RoleEntity detail(Long roleId) {
        return roleMapper.detail(roleId);
    }

    @Override
    public int count(RoleEntity roleEntity) {
        return roleMapper.count(roleEntity);
    }

    @Override
    public int insert(RoleEntity roleEntity) {
        return roleMapper.insertRole(roleEntity);
    }

    @Override
    public int update(RoleEntity roleEntity) {
        return roleMapper.updateRole(roleEntity);
    }

    @Override
    public int delete(Long roleId) {
        if(roleMapper.aollowDelRole(roleId)>0) {
            return  -1;
        }else{
            return roleMapper.deleteRole(roleId);
        }
    }
}
